import Vue from 'vue'
import axios from 'axios';
import QS from 'qs';
import Bridge from '@/plugins/bridge.js'
import router from "@/router/index.js"
import { Dialog } from 'vant';
// 全局注册
Vue.use(Dialog);
// 环境的切换
if (process.env.NODE_ENV == 'local' || process.env.NODE_ENV == 'development' || process.env.NODE_ENV == 'test') {
    axios.defaults.baseURL = 'https://etransfer.dg-tom.com/';
} else if (process.env.NODE_ENV == 'beta') {
    axios.defaults.baseURL = 'http://pretransfer.51-dg.com/';
} else if (process.env.NODE_ENV == 'production') {
    axios.defaults.baseURL = 'https://transfer.dg-mall.com/';

}
// 请求超时时间
axios.defaults.timeout = 15000;
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// 请求拦截器
axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        return Promise.error(error);
    })

// 响应拦截器
axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    // 服务器状态码不是200的情况
    error => {
        if (error.response.status) {
            Dialog.alert({
                message: '网络请求超时，请重新尝试'
            }).then(() => {
                console.log(error.response)
            });
            return Promise.reject(error.response);
        }
    }
);
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
                params: params
            })
            .then(res => {
                let code = res.data.code;
                let msg = res.data.msg;
                if (code == 4032) {
                    Dialog.alert({
                        title: '提示',
                        message: msg
                    }).then(() => {
                        // 清除缓存
                        localStorage.clear();
                        if (window) {
                            router.push({ path: '/login' })
                        } else {
                            Bridge.callhandler('updateToken', msg, (response) => {
                                // 处理返回数据
                                console.log(msg)
                                console.log('JS Echo called with:' + response)
                            })
                        }
                    });
                    return
                }
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
    let data = QS.stringify(params);
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(res => {
                let code = res.data.code;
                let msg = res.data.msg;
                if (code == 4032) {
                    Dialog.alert({
                        title: '提示',
                        message: msg
                    }).then(() => {
                        console.log(window)
                            // 清除缓存
                        localStorage.clear();
                        if (window) {
                            router.push({ path: '/login' })
                        } else {
                            Bridge.callhandler('updateToken', msg, (response) => {
                                // 处理返回数据
                                console.log(msg)
                                console.log('JS Echo called with:' + response)
                            })
                        }
                    });
                    return
                }
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}